/*
 * Created by JFormDesigner on Fri Nov 22 11:04:25 CST 2024
 */

package com.hafu.view.kehu;

import java.awt.event.*;
import javax.swing.event.*;
import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;
import javax.swing.table.*;

/**
 * @author 姜在虞
 */
public class ManageKeHuFrm extends JFrame {
    private KhInfoService khInfoService=new KhInfoService();
    private int chooseld;
    public ManageKeHuFrm() {
        initComponents();
        fillTable();
    }
   private void fillTable(){
        DefaultTableModel dtm=(DefaultTableModel)table1.getModel();
        dtm.setRowCount(0);
        ArrayList<KhInfo> khinfoList =khInfoService.getKhInfoByName(new KhInfo());
        for (KhInfo khinfo:khinfoList){
            Object[] rowData={khinfo.getId(),khinfo.getName(),khinfo.getTel()};
            dtm.addRow(rowData);
        }

    }
    private void chaxunbutton1(ActionEvent e) {
        // chaxun
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        dtm.setRowCount(0);
        String name = textField1.getText();
        KhInfo khInfo = new KhInfo();
        khInfo.setName(name);
        ArrayList<KhInfo> khInfoByName = khInfoService.getKhInfoByName(khInfo);

        if (khInfoByName.size() > 0) {
            for (KhInfo khinfo1 : khInfoByName) {
                Object[] rowData = {khinfo1.getId(), khinfo1.getName(), khinfo1.getTel()};
                dtm.addRow(rowData);
            }
        }else {
            JOptionPane.showMessageDialog(null, "没有找到该客户");
        }
    }
    private void table1MouseClicked(MouseEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        chooseld=(int) dtm.getValueAt(row,0);
        String name = (String) dtm.getValueAt(row, 1);
        String tel = (String) dtm.getValueAt(row, 2);
        textField2.setText(name);
        textField3.setText(tel);
    }
    private void xiugaibutton2(ActionEvent e){
        String name=textField2.getText();
        String tel=textField3.getText();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int row = table1.getSelectedRow();
//        // 检查是否有选中的行
        if (row == -1) {
            JOptionPane.showMessageDialog(null, "请先选择要修改的行");
            return;
        }
        int id = (int) dtm.getValueAt(row, 0);
        if(StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null, "客户名称不能为空");
        }
        if(StringUtil.isEmpty(tel)){
            JOptionPane.showMessageDialog(null, "客户电话不能为空");
        }
        KhInfo khInfo=new KhInfo();
        Boolean updateKhInfo = khInfoService.updateKhInfo(khInfo);
        if(updateKhInfo){
            JOptionPane.showMessageDialog(null, "修改成功");
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, "修改失败");

        }
    }
    private void shanchubutton3(ActionEvent e) {
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row, 0);
        Boolean deleteKhInfo = khInfoService.deleteKhInfo(id);
        if(deleteKhInfo){
            JOptionPane.showMessageDialog(null, "删除成功");
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(null, "删除失败");
       }
    }

    private void fillTable(AncestorEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        textField1 = new JTextField();
        label1 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        xiugaibutton2 = new JButton();
        shanchubutton3 = new JButton();
        label4 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        chaxunbutton1 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5ba2\u6237\u8868\u5355");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5ba2\u6237\u540d\u5b57");

        //======== scrollPane1 ========
        {
            scrollPane1.addAncestorListener(new AncestorListener() {
                @Override
                public void ancestorAdded(AncestorEvent e) {
                    fillTable(e);
                }
                @Override
                public void ancestorMoved(AncestorEvent e) {}
                @Override
                public void ancestorRemoved(AncestorEvent e) {}
            });
            scrollPane1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                }
            });

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null},
                    {null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5ba2\u6237\u540d\u5b57");

        //---- xiugaibutton2 ----
        xiugaibutton2.setText("\u4fee\u6539");
        xiugaibutton2.addActionListener(e -> xiugaibutton2(e));

        //---- shanchubutton3 ----
        shanchubutton3.setText("\u5220\u9664");
        shanchubutton3.addActionListener(e -> shanchubutton3(e));

        //---- label4 ----
        label4.setText("\u5ba2\u6237\u7535\u8bdd");

        //---- chaxunbutton1 ----
        chaxunbutton1.setText("\u67e5\u8be2");
        chaxunbutton1.addActionListener(e -> chaxunbutton1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(79, 79, 79)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(label2, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE))
                            .addGap(160, 160, 160)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(xiugaibutton2)
                                .addComponent(shanchubutton3)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(22, 22, 22)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                            .addGap(42, 42, 42)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 173, GroupLayout.PREFERRED_SIZE)
                            .addGap(48, 48, 48)
                            .addComponent(chaxunbutton1, GroupLayout.PREFERRED_SIZE, 181, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(47, 47, 47)
                            .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 549, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(97, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(44, 44, 44)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(chaxunbutton1))
                    .addGap(29, 29, 29)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 277, GroupLayout.PREFERRED_SIZE)
                    .addGap(45, 45, 45)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(xiugaibutton2))
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
                    .addGap(22, 22, 22)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(shanchubutton3))
                    .addGap(30, 30, 30))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTextField textField1;
    private JLabel label1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JButton xiugaibutton2;
    private JButton shanchubutton3;
    private JLabel label4;
    private JTextField textField2;
    private JTextField textField3;
    private JButton chaxunbutton1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
